package com.froggyware.workbench;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.froggyware.tadpole.Tadpole;
import com.froggyware.tadpole.core.DataStoreConfigurator;
import com.froggyware.workbench.data.Info;

import java.io.File;
import java.util.Calendar;

public class Workbench extends Activity
{

    private String id;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    private void init() {

        String dir = Environment.getExternalStorageDirectory() + File.separator + "tadpole-db";
        DataStoreConfigurator config = new DataStoreConfigurator(dir);
        Tadpole.getInstance().getDataStore().setConfigurator(config);

        final TextView text = (TextView) findViewById(R.id.results);

        Button b1 = (Button) findViewById(R.id.write);
            b1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Info i = new Info();
                    i.setTime(Calendar.getInstance().getTime().toLocaleString());
                    Tadpole.getInstance().save(i);
                    id = i.getId();
                    text.setText("Write done.  Time was " + i.getTime());
            }
        });


        Button b2 = (Button) findViewById(R.id.read);
            b2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if(id != null) {
                        Info i = Tadpole.getInstance().load(id,Info.class);
                        text.setText("Read called. Write Time was on "+i.getTime());
                    }
                    //text.setText("Read called");
            }
        });

    }

    
    protected void onDestroy() {
        Tadpole.getInstance().drop();
    }

}
